package com.ynet.middleground.office.service.impl;

import com.ynet.core.common.ServiceResult;
import com.ynet.core.log.IfpLogger;
import com.ynet.file.service.FileService;
import com.ynet.middleground.office.bean.Office2PdfReq;
import com.ynet.middleground.office.service.OfficeService;
import com.ynet.middleground.office.utils.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;

/**
 * office处理类
 * @author yang
 */
@Service(version = "1.0.0")
public class OfficeServiceImpl implements OfficeService {

    @Autowired
    private FileService fileService;
    @Override
    public ServiceResult<String> word2Pdf(Office2PdfReq req) {

        ServiceResult<String> serviceResult = new ServiceResult<>();

        String folder = System.getProperty("java.io.tmpdir");
        String time = System.currentTimeMillis() + "";

        String baseName = "";
        File tmpFile = null;

        // 获取文件
        String fileName = req.getFileName();
        String fileUrl = req.getFileUrl();
        if (StringUtils.isNotEmpty(fileName)) {
            // 是否存在
            boolean isExists = fileService.fileExists("oss", fileName, "zhongtai");
            if (!isExists) {
                serviceResult.setError("OF00001", "文件不存在");
                return serviceResult;
            }

            baseName = FilenameUtils.getBaseName(fileName);
            String extension = FilenameUtils.getExtension(fileName);

            String localFileName = baseName + time + "." + extension;

            fileService.downloadFile("oss", fileName, "zhongtai", localFileName, folder);
            tmpFile = new File(folder + localFileName);
        } else if (StringUtils.isNotEmpty(fileUrl)) {
            baseName = FilenameUtils.getBaseName((fileUrl));
            String extension = FilenameUtils.getExtension(fileUrl);
            String localFile = folder + baseName + time + "." + extension;
            FileUtils.downloadFileFromUrl(fileUrl, localFile);

            tmpFile = new File(localFile);
        } else {
            serviceResult.setError("OF00002", "要转换的文件不存在");
        }

        if (!tmpFile.exists()) {
            IfpLogger.info("office", "找不到下载的文件, dir:", tmpFile.getPath());
            serviceResult.setError("OF00003", "下载文件不存在");
            return serviceResult;
        }
        // pdf路径
        String tempPdfPath = folder + baseName + time + ".pdf";

        // word2pdf
        FileUtils.word2Pdf(tmpFile.getPath(), tempPdfPath);

        File pdfFile = new File(tempPdfPath);
        if (!pdfFile.exists()) {
            IfpLogger.info("office", "找不到生成的pdf文件, dir:", tempPdfPath);
            serviceResult.setError("OF00004", "找不到生成的pdf文件");
            return serviceResult;
        }

        serviceResult.setResult(fileService.uploadFile("oss", "", tempPdfPath, baseName + ".pdf", "zhongtai"));

        return serviceResult;
    }
}
